<?php
require_once("../admin_config.inc.php");

class option
{
    private $qid = null;
    
    private $table = null;
    
    function __construct($ques_id)
    {
        // create a connection to the database
        $db = connect_db();
        
        $this->qid = mysql_real_escape_string($ques_id);
        
        $this->table = "options";
    }
    
    function getOptions()
    {
        $getopt = "select option_id,option_val from {$this->table} where ques_id = {$this->qid}";
        
        $getopt_res = mysql_query($getopt);
        $data = array();
        
        while($getopt_row = mysql_fetch_object($getopt_res))
        {
            array_push($data,$getopt_res);
        }
        
        return $data;
    }
    
    function addOption($option)
    {
        $opt = mysql_real_escape_string($option);
        $id = mt_rand();
        $data = array( 'ques_id' => $this->qid,
                        'option_id' => $id,
                        'option_val' => $opt );
        $keys = implode("`,`",array_keys($data));
        $values = implode("','",$data);
        
        $add_query = vsprintf("insert into {$this->table}(`%s`) values('%s')",array($keys,$values));
        $add_res = mysql_query($add_query);
        
        return $add_res;
    }
    
    function delOption($option_id)
    {
        $opt_id = mysql_real_escape_string($option_id);
        $del_query = "delete from {$this->table} where ques_id = {$this->qid} and option_id = {$opt_id}";
        
        $del_res = mysql_query($del_query);
        
        return $del_res;
    }
    
    function editOption($option_val,$option_id)
    {
        $opt_val = mysql_real_escape_string($option_val);
        $opt_id = mysql_real_escape_string($option_id);
        
        $edit_query = "update {$this->table} set option_val = '{$opt_val}' where option_id = {$opt_id} and ques_id = {$this->qid}";
        $edit_res = mysql_query($edit_query);
        
        return $edit_res;
    }
}
?>
